home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C24 / RTTIwithExceptions.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  675 b   |  29 lines

  1. //: C24:RTTIwithExceptions.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. #include <typeinfo>
  7. #include <iostream>
  8. using namespace std;
  9. class X { public: virtual ~X(){} };
  10. class B { public: virtual ~B(){} };
  11. class D : public B {};
  12.  
  13. int main() {
  14.   D d;
  15.   B & b = d; // Upcast to reference
  16.   try {
  17.     X& xr = dynamic_cast<X&>(b);
  18.   } catch(...) {
  19.     cout << "dynamic_cast<X&>(b) failed" 
  20.          << endl;
  21.   }
  22.   X* xp = 0;
  23.   try {
  24.     typeid(*xp); // Throws exception
  25.   } catch(bad_typeid) {
  26.     cout << "Bad typeid() expression" << endl;
  27.   }
  28. } ///:~
  29.